package com.dfiov.cloud.msmtools.service.business.impl;

import com.alibaba.fastjson.JSONObject;
import com.dfiov.cloud.msmtools.mapper.entity.msuser.BaseLoginAccountAuthDo;
import com.dfiov.cloud.msmtools.mapper.entity.msuser.BaseUserAtributesDo;
import com.dfiov.cloud.msmtools.mapper.msuser.MsUserMapper;
import com.dfiov.cloud.msmtools.service.business.UserService;
import com.dfiov.cloud.msmtools.vo.CheckUserResult;
import com.pateo.qingcloud.framework.common.base.BaseRestResponse;
import com.pateo.qingcloud.framework.common.statusCode.CommonStatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class UserImpl implements UserService {

    @Resource
    MsUserMapper userMapper;

    public BaseRestResponse<String> consistencyCheckAll(String phone) {
        List<CheckUserResult> checkUserResultList = new ArrayList<>();
        if ( StringUtils.isBlank(phone) ){
            return new BaseRestResponse<>(CommonStatusCode.PARAM_ERROR.getCode(), CommonStatusCode.PARAM_ERROR.getDetail(), phone);
        }

        List<BaseLoginAccountAuthDo> baseLoginAccountAuthList = userMapper.getLoginAccountAuth(phone);
        log.info("phone:"+phone);
        log.info("baseLoginAccountAuthList:"+JSONObject.toJSONString(baseLoginAccountAuthList));
        if ( CollectionUtils.isEmpty(baseLoginAccountAuthList) ) {
            return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(), "此手机未注册tsp系统", null);
        }

        // 1 检查登录主表的数据 dbuser.base_login_account_auth
        HashSet<String> userAccountSidSet = new HashSet<>();
        for ( BaseLoginAccountAuthDo baseLoginAccountAuth : baseLoginAccountAuthList ) {
            String loginUniqueKey = baseLoginAccountAuth.getUserAccountSid() + "|" + baseLoginAccountAuth.getLoginId() + "|" + baseLoginAccountAuth.getLoginType();
            CheckUserResult checkUserResult = new CheckUserResult();
            if ( Objects.equals(baseLoginAccountAuth.getUserAccountSid(), 0) ){
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(), "base_login_account_auth 表存在非法数据 - UserAccountSid:"+baseLoginAccountAuth.getUserAccountSid(), JSONObject.toJSONString(checkUserResultList));
            }

            if (userAccountSidSet.contains( loginUniqueKey)) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(), "base_login_account_auth 表存在重复数据 - loginUniqueKey:"+loginUniqueKey, JSONObject.toJSONString(userAccountSidSet));
            }

            checkUserResult.setPhone(baseLoginAccountAuth.getLoginId());
            checkUserResult.setUserAccountSid(baseLoginAccountAuth.getUserAccountSid());

            checkUserResultList.add(checkUserResult);
            userAccountSidSet.add(loginUniqueKey);
        }

        // 2 检查用户信息表的数据 dbuser.base_user_profile
        for ( CheckUserResult checkUserResult : checkUserResultList ) {
            List<Long> sids = userMapper.getProfileSid( phone, checkUserResult.getUserAccountSid() );
            if ( sids.isEmpty() ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_profile 表缺失数据 - mobile_phone:"+phone+",UserAccountSid:"+checkUserResult.getUserAccountSid(), JSONObject.toJSONString(checkUserResultList));
            } else if ( sids.size() > 1 ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_profile 表存在重复数据 - mobile_phone:"+phone+",UserAccountSid:"+checkUserResult.getUserAccountSid(), JSONObject.toJSONString(checkUserResultList));
            }

            List<String> mobiles = userMapper.getProfileMobile( checkUserResult.getUserAccountSid() );
            if ( mobiles.size() > 1 ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_profile 表存在重复数据 - UserAccountSid:"+checkUserResult.getUserAccountSid(), JSONObject.toJSONString(checkUserResultList));
            } else if (!Objects.equals(mobiles.get(0), phone)) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_profile 表存在错误数据 - UserAccountSid:"+checkUserResult.getUserAccountSid()+"所对应的mobile_phone应该是:"+phone, JSONObject.toJSONString(checkUserResultList));
            }

            checkUserResult.setSid(sids.get(0));
        }

        // 3 检查用户账号表的数据 dbuser.base_user_account
        for ( CheckUserResult checkUserResult : checkUserResultList ) {
            List<String> accountIds = userMapper.getAccountId( checkUserResult.getUserAccountSid() );
            if ( accountIds.isEmpty() ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_account 表缺失数据 - sid:"+checkUserResult.getUserAccountSid(), JSONObject.toJSONString(checkUserResultList));
            } else if (StringUtils.isEmpty(accountIds.get(0))) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_account 表数据非法 - account_id:"+accountIds.get(0), JSONObject.toJSONString(checkUserResultList));
            }

            int accountCount = userMapper.findbyAccountId( accountIds.get(0) );
            if ( accountCount > 1 ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_account 表存在重复数据 - account_id:"+accountIds.get(0), JSONObject.toJSONString(checkUserResultList));
            }

            checkUserResult.setAccountId(accountIds.get(0));
        }

        // 4 检查用户属性表的数据 dbuser.base_user_atributes
        for ( CheckUserResult checkUserResult : checkUserResultList ) {
            List<BaseUserAtributesDo> userAtributes = userMapper.getUserAtributes( checkUserResult.getSid() );
            if ( userAtributes.isEmpty() ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_atributes 表缺失数据 - user_sid:"+checkUserResult.getSid(), JSONObject.toJSONString(checkUserResultList));
            } else if ( userAtributes.size() > 1 ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_atributes 表存在重复数据 - user_sid:"+checkUserResult.getSid()+",attribute_code:"+userAtributes.get(0).getAttributeCode(), JSONObject.toJSONString(checkUserResultList));
            }

            int userAtributeCount = userMapper.findbyAttributeValue( userAtributes.get(0).getAttributeCode(), userAtributes.get(0).getAttributeValue() );
            if ( userAtributeCount > 1 ) {
                return new BaseRestResponse<>(CommonStatusCode.SYS_EXCEPTION.getCode(),
                        "base_user_atributes 表存在重复数据 - attribute_code:"+userAtributes.get(0).getAttributeCode()+",attribute_value:"+userAtributes.get(0).getAttributeValue(), JSONObject.toJSONString(checkUserResultList));
            }

            checkUserResult.setAttributeCode(userAtributes.get(0).getAttributeCode());
            checkUserResult.setAttributeValue(userAtributes.get(0).getAttributeValue());
        }

        return new BaseRestResponse<>(CommonStatusCode.SUCCESS0.getCode(), CommonStatusCode.SUCCESS0.getDetail(), JSONObject.toJSONString(checkUserResultList));
    }





}
